const app = getApp(),url = app.globalData.url;
Page({

  data: {
    x_id:1,//选中的id
    sansan:[
      {id:1,subTypeName:'全职'},{id:2,subTypeName:'兼职'}
    ],
    // 薪资类型
    xz_id:1,
    xztype:[
      {id:1,subTypeName:'月薪'},{id:2,subTypeName:'年薪'}
    ],
    position:'',//职位 
    // arr_zhiwei:[{id:1,name:'前端工程师',},{id:2,name:'PHP工程师',}],
    hangye:'',//行业
    // arr_hangye:[{id:3,name:'电子/互联网',},{id:4,name:'软件工程',}],
    // xinzi:'',//薪资index
    arr_xinzi1:[],    //{id:5,name:'500-700',},{id:6,name:'1000-1500',}
    city:'',//城市
    yxid:'',
    workType:'全职',
    xzType:'月薪',
  },
  xinziType(e){
    var that = this
    var id = e.currentTarget.dataset.id
    var name = e.currentTarget.dataset.name
    // var arr = that.data.sansan
    that.setData({xz_id:id,xzType:name})
  },
  xingzhi(e){
    var that = this
    var id = e.currentTarget.dataset.id
    var name = e.currentTarget.dataset.name
    // var arr = that.data.sansan
    that.setData({x_id:id,workType:name})
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.setData({yxid:options.id,})
    // var sansan = that.data.sansan//工作性质
    //获取求职状态列表   /dgdx-Api/dict/getFindjobState
    // wx.request({
    //   url:url+'dict/getFindjobState',
    //   method: "GET",
    //   data:{},
    //   success:function(res){
    //     console.log('1获取求职状态列表后才能渲染',res)
    //     that.setData({sansan:res.data.data})
    //     setTimeout(function(){
    //       that.detail(options)
    //     },800)
    //   }
    // })
    that.detail(options)
    
    //获取薪资范围列表   dgdx-Api/dict/getWageRangeState
    // wx.request({
    //   url:url+'dict/getWageRangeState',
    //   method: "GET",
    //   data:{},
    //   success:function(res){
    //     console.log(res)
    //     that.setData({arr_xinzi1:res.data.data})
    //   }
    // })
    
  },
  

  // 薪资
  // xinziChange: function (e) {
  //   console.log(e);
  //   var that = this;
  //   var val = e.detail.value//这个是  index
  //   var arr_xinzi1 = that.dasta.arr_xinzi1
  //   console.log('点击的职位 index------',val)

  //   that.setData({xinzi:val,})//页面赋值 title:name
  // },

  // 摄入职位
  positioninput(e){
    var that = this
    console.log(e)
    console.log(e.detail.value)
    that.setData({
      position:e.detail.value
    })
  },

  
  startWage(e){    
    var that = this
    that.setData({startWage:e.detail.value})
  },
  endWage(e){
    var that = this
    that.setData({endWage:e.detail.value})
  },
  del_list(e){
    wx.removeStorage({key: 'cityName',})
    wx.removeStorage({key: 'hangye',})
    wx.removeStorage({key: 'zhiwei',})
    console.log('删除meiyoujiekou ')
    console.log(e)
    var id = e.currentTarget.dataset.id
    wx.request({
      url: url + 'cvJobIntention/delete?id='+id,
      method: "DELETE",
      // data:{id:id },
      success:function(res){
        console.log("删除意向",res);
        var ret = res.data
        if(ret.code == 200){
          wx.showToast({
            title:'删除成功',
            icon:'none',
          })
          setTimeout(function(){
            wx.navigateBack({
              delta: 1,
            })
          },1000)
          
        }else{
          
          that.setData({sub:true})
          wx.showToast({
            title: '删除失败',
            icon:'none'
          })
        }
      }
    })
  },
  checkSensWords(e){
    let itemData = e.target.dataset;
    let label = Object.values(itemData)[0]
    let key = Object.keys(itemData)[0]
    this.sensWords = this.sensWords || {}
    app.validSensWords(e.detail.value, label).then(() => {
      delete this.sensWords[key]
    }).catch(() => {
      this.sensWords = {...itemData, ...this.sensWords}
    })
  },
  add_yixiang(){
    var that = this
    wx.removeStorage({key: 'cityName',})
    wx.removeStorage({key: 'hangye',})
    wx.removeStorage({key: 'zhiwei',})
    console.log('保存')
    var id = that.data.yxid
    if(id > 0){
      var method = "put"
      var urlapi = "cvJobIntention/update"
    }else{
      var method = "POST"
      var urlapi = "cvJobIntention/add"
      // var urlapi = "cvJobIntention/add"
    }
    var user_uid = wx.getStorageSync('user_massage')
    var position = that.data.position 
    var industry = that.data.hangye//hangye
    var workplace = that.data.city//城市
    var startWage = that.data.startWage//最低
    var endWage = that.data.endWage//最高
    var workType = that.data.workType//工作性质
    var wageType = that.data.xzType//薪资类型，1：月薪，2：年薪，3: 周新。4：时薪
    if(position == '' || position == undefined){
      wx.showToast({
        title: '请输入职位',
        icon:'none',
      })
      return false
    }
    if(industry == '' || industry == undefined){
      wx.showToast({
        title: '请选择行业',
        icon:'none',
      })
      return false
    }
    if(workplace == '' || workplace == undefined){
      wx.showToast({
        title: '请选择城市',
        icon:'none',
      })
      return false
    }
    if(workType == '' || workType == undefined){
      wx.showToast({
        title: '请选择工作性质',
        icon:'none',
      })
      return false
    }
    if(startWage == '' || startWage == undefined || endWage == '' || endWage == undefined){
      wx.showToast({
        title: '请检查工资信息',
        icon:'none',
      })
      return false
    }
    let sensWords = Object.values(this.sensWords || {})
    if(sensWords.length > 0){
      wx.showToast({
        title: `${sensWords.join(',')}包含敏感词，请修改后再保存`,
        icon:'none',
      })
			return false;
    }
    var data ={
      approval: '1',
      createTime:'',//创建时间
      endWage:endWage,//最高薪资
      industry:industry,//行业industry
      position:position,//意向职位position
      workplace:workplace,//工作地点
      workType:workType,//工作类型
      startWage:startWage,//最低薪资
      updateTime:'',//更新使劲按
      userUid:user_uid.userUid,//用户id userUid
      wageType:wageType,//薪资类型 薪资类型，1：月薪，2：年薪，3周新。4：时薪
      id:id,//求职意向id
    }
    wx.request({
      url:url+urlapi,
      header:{
      'context-type': 'application/json',
      },
      method: method,//json.parse()
      data:data,
      success:function(res){
        var ret = res.data
        console.log(res)
        if(ret.code == 200){

          wx.navigateBack({
            delta: 1,
          })
        }else{
          wx.showToast({
            title:'保存失败',
            icon:'none',
          })
        }
      }
    })

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    // that.xinzi()
    var city = wx.getStorageSync('cityName')
    var hangye = wx.getStorageSync('hangye')
    var zhiwei = wx.getStorageSync('zhiwei')
    console.log('zhiwei',zhiwei)
    if(city){
      that.setData({city:city})
    }
    if(hangye){
      that.setData({hangye:hangye})
    }
    if(zhiwei){
      that.setData({position:zhiwei})
    }
    // that.setData({city:city,hangye:hangye,position:zhiwei})
  },
  //职位选择
  zwlist(){
    wx.navigateTo({
      url: '/HomePage/zwlist/zwlist',
    })
  },
  //行业选择
  industry(){
    wx.navigateTo({
      url: '/HomePage/industry/industry',
    })
  },
  // 选择城市
  select_city(){
    wx.navigateTo({
      url: '/HomePage/selectcity/selectcity',
    })
  },

  detail(options){
    var that = this
    var sansan = that.data.sansan
    var xztype = that.data.xztype//薪资类型
    if(Object.keys(options).length  == 0){
      return false
    }else{
      wx.request({
        url:url+'cvJobIntention/getCVJobIntentById',
        method: "GET",
        data:{
          id:options.id
        },
        success:function(res){
          console.log("2求职意向信息",res);
          var ret = res.data
          if(ret.code == 200){
            // var startWage = ret.data.startWage//薪资范围
            var workType = ret.data.workType//工作性质
            var wageType = ret.data.wageType//薪资性质
            for(let type in sansan){
              console.log(type)
              if(sansan[type].subTypeName == workType){
                that.setData({
                  x_id:sansan[type].id
                })
                break;
              }
            }
            for(let type in xztype){
              console.log(type)
              if(xztype[type].subTypeName == wageType){
                that.setData({
                  xz_id:xztype[type].id
                })
                break;
              }
            }
            that.setData({
              position:ret.data.position,//职位
              hangye:ret.data.industry,//行业
              city:ret.data.workplace,//城市
              startWage:ret.data.startWage,
              endWage:ret.data.endWage,
              workType:workType,
              xzType:wageType
            })
          }else{
            wx.showToast({
              title: '获取失败',
              icon:'none',
            })
          }
        }
      })
    }
  },



  // // 职位
  // zhiweiChange: function (e) {
  //   // console.log(e);
  //   var that = this;
  //   var val = e.detail.value//这个是  index
  //   console.log('点击的职位 index------',val)
  //   // var name = that.data.arr_zhiwei[val].name
  //   // var cateid = that.data.arr_zhiwei[val].id
  //   // that.data.where.cate_id = cateid;//店铺里的 分类id
  //   // that.setData({ where: that.data.where });//条件赋值
  //   that.setData({index:val,})//页面赋值 title:name
  // },
  // //行业
  // hangyeChange: function (e) {
  //   // console.log(e);
  //   var that = this;
  //   var val = e.detail.value//这个是  index
  //   console.log('点击的职位 index------',val)
  //   // var name = that.data.arr_zhiwei[val].name
  //   // var cateid = that.data.arr_zhiwei[val].id
  //   // that.data.where.cate_id = cateid;//店铺里的 分类id
  //   // that.setData({ where: that.data.where });//条件赋值
  //   that.setData({hangye:val,})//页面赋值 title:name
  // },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})